﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;

namespace ServCSS.ServCSS.Cliente
{
    class CCliente
    {
        public CCliente_datos data { get; set; }

        public CCliente(string host, int puerto)
        {
            try
            {
                data = new CCliente_datos();
                data.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                nuevoSock(host, puerto);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());   
            }
        }

        public void nuevoSock (string host, int puerto)
        {
            try
            {
                Console.WriteLine("cli_Root: intentando conectar Socket");
                if (data.socket.Connected)
                {
                    data.socket.Close();
                }

                data.host = IPAddress.Parse(host);
                IPEndPoint endPoint = new IPEndPoint(data.host, puerto);

                data.socket.Connect(endPoint);
                Console.WriteLine("cli_Root: Socket Conectado.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public void nuevoSock(IPAddress host, int puerto)
        {
            try
            {
                Console.WriteLine("cli_Root: intentando conectar Socket");
                if (data.socket.Connected)
                {
                    data.socket.Close();
                    data.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }

                data.host = host;
                IPEndPoint endPoint = new IPEndPoint(data.host, puerto);

                data.socket.Connect(endPoint);
                Console.WriteLine("cli_Root: Socket Conectado.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public void Run()
        {
            
        }

    }
}
